// Playground - noun: a place where people can play

import Cocoa

print("hello ")
struct HTTPRequest
{
    enum Method
    {
        case POST
        case GET
    }
    
    var path : String
    var method : Method
}

protocol HTTPHnadleType
{
    typealias DATA
    func handler (request: HTTPRequest , data: DATA) -> Bool
}

class HTTPServer
{
    private var handlers: [(request: HTTPRequest , data: Any) -> Bool ] = []
    
    func addHandler<T HTTPHnadleType>(handle: T) {
        handlers.append { (request: HTTPRequest , data: Any) -> Bool in
            if  let typedArg = data as? T.DATA {
                return handle.handler (request,typedArg)
            }
            return false
         }
    }
    
    func dispatch(req:HTTPRequest, data: Any) -> Bool
    {
        for handle in handlers
        {
            if handle(request: req, data: data)
            {
                return true
            }
        }
        return false
    }
}


class MyHandle : HTTPHnadleType {
    func handler(request: HTTPRequest, data: Int) ->Bool {
        return data > 5
    }
}

let server = HTTPServer()
server.addHandler(MyHandle())
server.dispatch(  HTTPRequest(path: "test", method: .POST), data: 5)



